//
//  SetView.m
//  dhfsAPP
//
//  Created by Devil on 2017/8/2.
//  Copyright © 2017年 Devil. All rights reserved.
//

#import "SetView.h"
#import "fsapp-Swift.h"
@interface SetView()

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)NSArray *cellName;

@end

@implementation SetView
#pragma mark - 懒加载
-(HeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(128)) Title:@"设置"];
    }
    
    return _headerView;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT(128), SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT(128))];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.backgroundColor = [UIColor whiteColor];
        
        _tableView.scrollEnabled = NO;
    }
    
    return _tableView;
}

-(NSArray *)cellName{
    if (!_cellName) {
        _cellName = [NSArray arrayWithObjects:@"编辑资料",@"清除收藏",@"清除缓存",nil];
    }
    
    return _cellName;
}
#pragma mark - 构造函数
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self initUI];
    }
    
    return self;
}

#pragma mark - UITableViewDelegate&UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT(101);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return HEIGHT(905);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *str = @"cell";
    UITableViewCell *cell = nil;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    else
    {
        while ([cell.contentView.subviews lastObject] != nil)
        {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    

    cell.selectionStyle = NO;
    
    cell.backgroundColor = RGB(221,221, 221);

    UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(100))];
    
    cellView.backgroundColor = [UIColor whiteColor];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(47), 0, 0, 0)];
    
    title.text = self.cellName[indexPath.row];
    title.textColor = RGB(51, 51, 51);
    title.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
    
    [title sizeToFit];
    title.center = CGPointMake(title.center.x, cellView.center.y);
    
    [cellView addSubview:title];
    
    if (indexPath.row == 0) {
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH(681), 0, WIDTH(23), HEIGHT(43))];
        
        imgV.image = [UIImage imageNamed:@"更多"];
        
        imgV.center = CGPointMake(imgV.center.x, cellView.center.y);
        
        [cellView addSubview:imgV];
    }
    
    [cell addSubview:cellView];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(905))];
    
    footerView.backgroundColor = [UIColor clearColor];
    
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    logoutBtn.frame = CGRectMake(WIDTH(44), HEIGHT(693), WIDTH(660), HEIGHT(98));
    
    [logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    
    logoutBtn.backgroundColor = RGB(235, 24, 116);
    
    [logoutBtn addTarget:self action:@selector(clickLogoutBtn) forControlEvents:UIControlEventTouchUpInside];
    
    logoutBtn.layer.masksToBounds = YES;
    logoutBtn.layer.cornerRadius = 5.0;
    
    [footerView addSubview:logoutBtn];
    
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (indexPath.row == 0)
        {
             SetNameViewController *vc = [[SetNameViewController alloc] init];
           UINavigationController *nav =
            [[UINavigationController alloc] initWithRootViewController:vc];
            
            
            if ([self.setViewDelegate respondsToSelector:@selector(pushVC:)])
            {
                [self.setViewDelegate pushVC:nav];
            }
        }
        else if(indexPath.row == 1)
        {
            if ([self.setViewDelegate respondsToSelector:@selector(deleteCollect)])
            {
                [self.setViewDelegate deleteCollect];
            }
        }
        else if (indexPath.row == 2)
        {
            if ([self.setViewDelegate respondsToSelector:@selector(deleteCache)])
            {
                [self.setViewDelegate deleteCache];
            }
        }
    });
}

#pragma mark - 其他函数
-(void)initUI{
    [self addSubview:self.headerView];
    
    [self addSubview:self.tableView];
}

-(void)clickLogoutBtn{
    if ([self.setViewDelegate respondsToSelector:@selector(logout)]) {
        [self.setViewDelegate logout];
    }
}
@end
